package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.DuhaiRonduController;
import com.antoco.operation.bigscreen.model.dto.DuhaiRonduDTO;
import com.antoco.operation.bigscreen.model.entity.DuhaiRondu;
import com.antoco.operation.bigscreen.second.ValidList;
import com.antoco.operation.bigscreen.mapper.DuhaiRonduMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.DuhaiRonduService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/duhai-rondu")
@RestController
public class DuhaiRonduControllerImpl implements DuhaiRonduController {

    @Autowired
    private DuhaiRonduService duhaiRonduService;

    @Autowired
    private DuhaiRonduMapper duhaiRonduMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public DuhaiRonduDTO save(@Validated @RequestBody DuhaiRonduDTO duhaiRonduDTO) {
        DuhaiRondu duhaiRondu = duhaiRonduMapper.asEntity(duhaiRonduDTO);
        duhaiRonduService.save(duhaiRondu);
        channelDataService.pushAllLists(this.getClass().getName());
        return duhaiRonduDTO;
    }

    @Override
    @PostMapping("/save-lists")
    @ResponseStatus(HttpStatus.CREATED)
    public List<DuhaiRonduDTO> saveLists(@RequestBody ValidList<DuhaiRonduDTO> duhaiRonduDTOLists) {
        duhaiRonduService.save(duhaiRonduMapper.asEntityList(duhaiRonduDTOLists));
        //当完成数据的操作后，调用该封装的方法获取变化后的数据进行推送
        channelDataService.pushAllLists(this.getClass().getName());
        return duhaiRonduDTOLists;
    }

    @Override
    @GetMapping("/{id}")
    public DuhaiRonduDTO findById(@PathVariable("id") String id) {
        DuhaiRondu duhaiRondu = duhaiRonduService.findById(id).orElse(null);
        return duhaiRonduMapper.asDTO(duhaiRondu);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        duhaiRonduService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<DuhaiRonduDTO> list() {
        return duhaiRonduMapper.asDTOList(duhaiRonduService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<DuhaiRonduDTO> pageQuery(Pageable pageable) {
        Page<DuhaiRondu> duhaiRonduPage = duhaiRonduService.findAll(pageable);
        List<DuhaiRonduDTO> dtoList = duhaiRonduPage
                .stream()
                .map(duhaiRonduMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, duhaiRonduPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public DuhaiRonduDTO update(@RequestBody DuhaiRonduDTO duhaiRonduDTO, @PathVariable("id") String id) {
        DuhaiRondu duhaiRondu = duhaiRonduMapper.asEntity(duhaiRonduDTO);
        duhaiRonduService.update(duhaiRondu, id);
        channelDataService.pushAllLists(this.getClass().getName());
        return duhaiRonduDTO;
    }
}